package baiqitun.stupro.project.service.impl;

import baiqitun.stupro.common.exception.enums.support.ExistedExceptionEnum;
import baiqitun.stupro.common.exception.enums.support.NotFoundExceptionEnum;
import baiqitun.stupro.common.exception.support.NotFoundException;
import baiqitun.stupro.common.exception.support.RelationExistedException;
import baiqitun.stupro.common.service.support.MyServiceImpl;
import baiqitun.stupro.project.entity.SysProject;
import baiqitun.stupro.project.entity.SysProjectUser;
import baiqitun.stupro.project.mapper.SysProjectUserMapper;
import baiqitun.stupro.project.service.SysProjectService;
import baiqitun.stupro.project.service.SysProjectUserService;
import baiqitun.stupro.security.enums.SysRoleEnum;
import baiqitun.stupro.security.entity.SysUser;
import baiqitun.stupro.security.service.SysUserRoleService;
import baiqitun.stupro.security.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 项目成员表 服务实现类
 * </p>
 *
 * @author kedaji
 * @since 2020-04-17
 */
@Service
public class SysProjectUserServiceImpl extends MyServiceImpl<SysProjectUserMapper, SysProjectUser> implements SysProjectUserService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysProjectService sysProjectService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public void checkRelationExisted(String projectId, String userId) {
        SysProjectUser queryProjectUser = new SysProjectUser();
        queryProjectUser.setProjectId(projectId).setUserId(userId);
        SysProjectUser relation = getOne(new QueryWrapper<>(queryProjectUser));
        if (relation != null){
            throw new RelationExistedException(ExistedExceptionEnum.PROJECT_USER_RELATION_EXISTED).put(SysProject.class, "id", projectId).put(SysUser.class, "id", userId);
        }
    }

    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public SysProjectUser getExistedRelation(String projectId, String userId) {
        SysProjectUser queryProjectUser = new SysProjectUser();
        queryProjectUser.setProjectId(projectId).setUserId(userId);
        SysProjectUser relation = getOne(new QueryWrapper<>(queryProjectUser));
        if (relation == null){
            throw new NotFoundException().put(SysProject.class, "id", projectId).put(SysUser.class, "id", userId);
        }
        return relation;
    }

    @Transactional(readOnly = true, rollbackFor = Exception.class)
    @Override
    public List<SysUser> listTutorByProjectId(String projectId) {
        sysProjectService.checkNotFound(projectId, new NotFoundException(NotFoundExceptionEnum.PROJECT_NOT_FOUND));
        SysProjectUser queryProjectUser = new SysProjectUser();
        queryProjectUser.setProjectId(projectId);
        List<SysProjectUser> projectUsers = list(new QueryWrapper<>(queryProjectUser));
        List<SysUser> tutors = new LinkedList<>();
        for (SysProjectUser relation: projectUsers){
            sysUserService.checkNotFound(relation.getUserId(), new NotFoundException(NotFoundExceptionEnum.USER_NOT_FOUND));
            if (sysUserRoleService.hasRole(relation.getUserId(), SysRoleEnum.ROLE_TUTOR)){
                SysUser user = sysUserService.getById(relation.getUserId());
                tutors.add(sysUserService.getById(user.getId()));
            }
        }
        return tutors;
    }
}
